<?php
/**
 * Created by PhpStorm.
 * User: vlinked
 * Date: 2018/8/10
 * Time: 14:54
 */

require_once "../../../api/socket_config.php";
require_once '../../a_config.php';
require_once "{$A_CONFIG['api_include']}";
require_once '../../api_header.php';
$conn = connect_to_db();
$err = -1;
$msg = "";
$condition = array();
if (isset($_POST['token']) && !empty($_POST["token"]) &&
    isset($_POST['time']) && !empty($_POST["time"]) &&
    isset($_POST['box_id']) && !empty($_POST["box_id"])) {

    $key = $A_CONFIG['api_key'];
    $unixtime = $_POST['time'];
    $token = md5($key . $unixtime);
    if ($token == $_POST["token"]) {

        $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
        socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, array("sec" => 1, "usec" => 0));
        socket_set_option($socket, SOL_SOCKET, SO_SNDTIMEO, array("sec" => 6, "usec" => 0));

        if (socket_connect($socket, SHARE_BATTERY_IP, SHARE_BATTERY_PORT) == false) {
            $err = -3;
            $msg = "连接服务器失败";
        } else {
            $box_id = $_POST["box_id"];
            $key = SHARE_BATTERY_SS_KEY;
            $time = time();
            $service = "adminTerminal";//指令
            $info = compact('box_id', 'service');
            $token = md5($key . $time . json_encode($info));
            $json = json_encode(compact("info", "time", "token"), JSON_UNESCAPED_UNICODE);
            socket_write($socket, $json, strlen($json));
            $string = socket_read($socket, 8192);
            @ $arr = json_decode($string, true);
            socket_close($socket);//工作完毕，关闭套接流
            if (isset($arr) && is_array($arr)) {
                $now = time();
                $info = $arr['info'];
                $time = $arr['time'];
                $token = $arr['token'];
                if ($now - 60 < $time && $time < $now + 60) {
                    $row_str = $key . $time . json_encode($info);
                    $my_token = md5($row_str);
                    if ($token == $my_token) {
                        //验证通过
                        $code = $arr['code'];
                        if ($code = '0000') {//借充电宝指令发送成功
                            $Slot = $arr['info']['Slot'];
                            $TerminalID = $arr['info']['TerminalID'];
                            $trans_id = $arr['info']['trans_id'];

                            $api_url = "http://didao.lovemojito.com/e_icbcZjLongYan/share_battery/GatewayWorker/Applications/ShareBattery/api/apiQureyAdminGetResult.php?trans_id=" . $trans_id;
                            $resultStr = fetchUrlContent($api_url);
                            $result = json_decode($resultStr, true);

                            if ($result['err'] == '0000') {//成功 开始处理订单
                                $err = 0;
                                $msg = '弹出成功';
                            } else {
                                $err = -2;
                                $msg = "弹出失败";
                            }

                        } else {
                            $err = -2;
                            $msg = "指令发送失败";
                        }
                    } else {
                        $err = -2;
                        $msg = "验证失败";
                    }
                } else {
                    $err = -2;
                    $msg = "超时";
                }
            } else {
                $err = -2;
                $msg = "无响应";
            }


        }


    } else {
        $err = -4;
        $msg = "token验证失败";
    }
} else {
    $err = -5;
    $msg = "参数错误";
}

ob_clean();
$res_arr = compact('err', 'msg','resultStr');
echo json_encode($res_arr);

